<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #box {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <input type="button" value="开始" id="btn1">
    <input type="button" value="停止" id="btn2">
    <div id="box"></div>
    <script>
        var btn1 = document.getElementById("btn1")
        var btn2 = document.getElementById("btn2")
        var box = document.getElementById("box")

        var timer
        var time = 2000
        var end = 1000
        var interval = 10

        var maxcount = time / interval
        var count = 0
        var nowLeft = box.offsetLeft

        var step = (end - nowLeft) / maxcount
        btn1.onclick = function () {
            clearInterval(timer)
            timer = setInterval(function () {
                nowLeft += step
                count++
                if (count >= maxcount) {
                    nowLeft = end
                }
                box.style.left = nowLeft + "px"
            }, interval)
        }

        btn2.onclick = function () {
            clearInterval(timer)
        }
    </script>

</body>

</html>